import axios from "axios"
import querystring from "querystring"

const instance = axios.create({
	//网络的公共配置
	timeout:5000
})
const errorHandle = (status, info) => {
	switch (status) {
		case 404:
			console.log("地址错误");
			break;
		default:
			console.info(info);
			break;
	}
}
//发送数据之前
instance.interceptors.request.use(
	config => {
		if (config.methods === "post") {
			config.data = querystring.stringify(config.data)
		}
		return config;
	},
	error => {
		return Promise.reject(error)
	}
)

//获取数据之前
instance.interceptors.response.use(
	response => {
		return response.status === 200 ? Promise.resolve(response) : Promise.reject(response);
	},
	error => {
		const { response } = error;
		errorHandle(response.status, response.info);
	}
)
export default instance;